<?php
/*
  Plugin Name: Bài viết cùng chuyên mục
  Description: Hiển thị các bài viết trong cùng 1 chuyên mục tại sidebar
  Author: Nguyen Binh Nguyen
 */

// Start class category_posts_widget //

class category_posts_widget extends WP_Widget {

  /**
   * Constructor
   */
  function category_posts_widget() {
    $widget_ops = array('classname' => 'category_posts_widget', 'description' => 'Các bài viết trong Chuyên mục được chọn để hiển thị'); // Widget Settings
    $control_ops = array('id_base' => 'category_posts_widget'); // Widget Control Settings
    $this->WP_Widget('category_posts_widget', 'Các bài viết theo chuyên mục', $widget_ops, $control_ops); // Create the widget
  }

  /**
   * Extract Args 
   * (non-PHPdoc)
   * @see WP_Widget::widget()
   */
  function widget($args, $instance) {
    extract($args);
    $title = apply_filters('widget_title', $instance['title']); // the widget title
    $cp_id = $instance['cp_id']; // category id
    $postsnumber = $instance['postsnumber']; // number of posts to display
    // $rssid 		= $instance['rssid']; // rss feed link
    // $newsletterurl 	= $instance['newsletter_url'];  URL of newsletter signup
    // $authorcredit	= isset($instance['author_credit']) ? $instance['author_credit'] : false ; give plugin author credit
// Before widget //
    echo $before_widget;
    // Title of widget //
    if ($title) {
      echo $before_title . $title . $after_title;
    }
    // Widget output //
    ?>
    <!--  Start list  -->
    <div class="catbox-row clear"><ul>
        <?php
        query_posts('showposts=' . $postsnumber . '&cat=' . $cp_id);
        $postcount = 0;
        while (have_posts()) : the_post();
          global $post;
//				if($postcount == 0 ) {  
          ?>
          <li>
            <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'themejunkie'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a>

            <p class="meta-date">(<?php the_time('d.M.Y'); ?>)</p>

            <?php
            if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
              the_post_thumbnail(array(200, 160), array('class' => 'alignleft post_thumbnail'));
            }
            ?>

            <!--end .entry-meta-->
            <div class="entry-excerpt">
              <?php //the_excerpt( __( 'more <span class="meta-nav">&rarr;</span>', 'themejunkie' ) );  ?>
              <?php //echo	 bm_better_excerpt(400,'...');  ?>
            </div> <!--end .entry-excerpt-->				
          </li> <!--end .first-->
          <div  style="clear: both"></div>
          <?php
          $postcount++;
        endwhile;
        wp_reset_query();
        ?>
      </ul></div><!--end .catbox-->
    <div class="clear"></div>
    <!--  End List -->
    <?php
    // After widget //
    echo $after_widget;
  }

  /**
   * Update Settings 
   * (non-PHPdoc)
   * @see WP_Widget::update()
   */
  function update($new_instance, $old_instance) {
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['cp_id'] = strip_tags($new_instance['cp_id']);
    $instance['postsnumber'] = strip_tags($new_instance['postsnumber']);
    return $instance;
  }

  /**
   * Widget Control Panel Form
   * (non-PHPdoc)
   * @see WP_Widget::form()
   */
  function form($instance) {
    $defaults = array('title' => 'Category Posts', 'cp_id' => 'Category ID');
    $instance = wp_parse_args((array) $instance, $defaults);
    ?>
    <p>
      <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
      <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
    </p>
    <p>
      <label for="<?php echo $this->get_field_id('cp_id'); ?>"><?php _e('Category ID'); ?></label>
      <select id="<?php echo $this->get_field_id('cp_id'); ?>" name="<?php echo $this->get_field_name('cp_id'); ?>" > 
        <option value="">Select a Category</option> 
        <?php
        $category = $instance['cp_id'];
        $categories = get_categories();
        foreach ($categories as $cat) {
          $option = '<option value="' . $cat->term_id . '"';
          if ($category == $cat->term_id)
            $option .= ' selected="selected">';
          else {
            $option .= '>';
          }
          $option .= $cat->cat_name;
          $option .= ' (' . $cat->category_count . ')';
          $option .= '</option>';
          echo $option;
        }
        ?>
      </select>
    </p> 		
    <p>
      <label for="<?php echo $this->get_field_id('postsnumber'); ?>"><?php _e('Number of posts to display'); ?></label>
      <input class="widefat" id="<?php echo $this->get_field_id('postsnumber'); ?>" name="<?php echo $this->get_field_name('postsnumber'); ?>" type="text" value="<?php echo $instance['postsnumber']; ?>" />
    </p>
    <?php
  }

}

// End class category_posts_widget
add_action('widgets_init', create_function('', 'return register_widget("category_posts_widget");'));
?>